package animation;

import java.awt.DisplayMode;
import java.awt.Graphics;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JFrame;

import screen.Screen;

public class BuckyAnimation {
	public static void main(String[] args) {
		DisplayMode displayMode = new DisplayMode(1024,768,16,DisplayMode.REFRESH_RATE_UNKNOWN);
		BuckyAnimation b = new BuckyAnimation();
		b.run(displayMode);
	}
	
	private Screen screen;
	private Image bg;
	private Animation a = new Animation();
	
	public void loadPics(){
		bg = new ImageIcon("img/bg.jpg").getImage();
		Image f1 = new ImageIcon("img/f1.jpg").getImage();
		Image f2 = new ImageIcon("img/f2.jpg").getImage();
		a.addScene(f1, 250);
		a.addScene(f2, 250);
	}
	
	public void run(DisplayMode dm){
		screen = new Screen();
		try{
			screen.setFullScreen(dm, new JFrame());
			loadPics();
			movieLoop();
		}finally{
			screen.restoreScreen();
		}
	}
	
	public void movieLoop(){
		long startingTime = System.currentTimeMillis();
		long cumTime = startingTime;
		
		while(cumTime - startingTime < 5000){
			long timePassed = System.currentTimeMillis() - cumTime;
			cumTime += timePassed;
			a.update(timePassed);
			
			Graphics g = screen.getFullScreenWindow().getGraphics();
			draw(g);
			g.dispose();
			
			try{
				Thread.sleep(20);
			}catch(Exception e){}
			
		}
	}
	
	public void draw(Graphics g){
		g.drawImage(bg,0,0,null);
		g.drawImage(a.getImage(),0,0,null);
	}
}
